package com.qujia.station.controller;


import com.alibaba.fastjson.JSONObject;
import com.qujia.common.constant.UserConstants;
import com.qujia.common.core.controller.BaseController;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.core.page.PageDomain;
import com.qujia.common.core.page.TableDataInfo;
import com.qujia.common.core.page.TableSupport;
import com.qujia.common.exception.BaseException;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.station.domain.InventoryReportDaily;
import com.qujia.station.domain.Station;
import com.qujia.station.service.IStationService;
import com.qujia.station.service.InventoryReportDailyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Api(tags = "进存销日报表")
@RestController
@RequestMapping("/inventoryReportDaily")
@Slf4j
public class InventoryReportDailyController extends BaseController {

    @Autowired
    private InventoryReportDailyService inventoryReportDailyService;

    @Autowired
    private IStationService stationService;

    @PostMapping("/list")
    @ApiOperation(value = "查询进销存日报表", httpMethod = "POST")
    public TableDataInfo selectInventoryReportDailyList(@RequestBody InventoryReportDaily inventoryReportDaily){
        if(Objects.isNull(inventoryReportDaily) || Objects.isNull(inventoryReportDaily.getPageSize())
                || Objects.isNull(inventoryReportDaily.getPageNum())){
            return failDataTable();
        }

        SysUser user = SecurityUtils.getLoginUser().getUser();
        if(Objects.isNull(user)){
            return failUserTable();
        }
        getStationList(inventoryReportDaily,user);
        log.info("查询进销存日报表selectInventoryReportDailyList请求参数:[{}]", JSONObject.toJSONString(inventoryReportDaily));
        PageDomain pageDomain = TableSupport.buildPageRequest();
        pageDomain.setPageNum(inventoryReportDaily.getPageNum());
        pageDomain.setPageSize(inventoryReportDaily.getPageSize());
        startPage(pageDomain);
        List<InventoryReportDaily> inventoryReportDailies = inventoryReportDailyService.selectInventoryReportDailyList(inventoryReportDaily);
        log.info("查询进销存日报表selectInventoryReportDailyList响应结果:[{}]", inventoryReportDailies.size());
        return getDataTable(inventoryReportDailies);
    }

    private void getStationList(InventoryReportDaily inventoryReportDaily, SysUser user){
        if(UserConstants.LEVEL_TWO.equals(user.getUserLevel())){
            if(Objects.nonNull(inventoryReportDaily) && Objects.nonNull(inventoryReportDaily.getStationId())){
                return;
            }
            List<Station> stationList = stationService.getStationByEnterpriseId(user.getEnterpriseId());
            if(CollectionUtils.isEmpty(stationList)){
                throw new BaseException("油企不存在");
            }
            List<Long> stationIdList = new ArrayList<>();
            for (Station station : stationList) {
                stationIdList.add(station.getStationId());
            }
            inventoryReportDaily.setStationIdList(stationIdList);
        } else if(UserConstants.LEVEL_THREE.equals(user.getUserLevel())){
            inventoryReportDaily.setStationId(user.getStationId());
        }
    }

    @PostMapping("/sum")
    @ApiOperation(value = "汇总进销存日报表", httpMethod = "POST")
    public AjaxResult inventoryReportDailySum(@RequestBody InventoryReportDaily inventoryReportDaily){
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if(Objects.isNull(user)){
            return AjaxResult.error("用户不存在，请重新登录");
        }
        getStationList(inventoryReportDaily,user);
        log.info("汇总进销存日报表inventoryReportDailySum 请求参数：[{}]",JSONObject.toJSONString(inventoryReportDaily));
        InventoryReportDaily inventoryReportDailySum = inventoryReportDailyService.inventoryReportDailySum(inventoryReportDaily);
        log.info("汇总进销存日报表inventoryReportDailySum 响应结果：[{}]",JSONObject.toJSONString(inventoryReportDailySum));
        return AjaxResult.success(inventoryReportDailySum);
    }


    @GetMapping("/task")
    @ApiOperation(value = "进销存日报表定时任务手动请求", httpMethod = "GET")
    public AjaxResult inventoryReportDailyTask(){
        inventoryReportDailyService.inventoryReportDailyStatistics();
        return AjaxResult.success();
    }


}
